找传奇、传世资源到传世资源站!

C# 上传文件至阿里云OSS实例源码下载

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

C# 上传文件至阿里云OSS实例源码下载 C#语言基础-第1张C# 上传文件至阿里云OSS实例源码下载 C#语言基础-第2张using System;using System.Collections.Generic;using System.Linq;using System.Web;using Aliyun.OpenServices.OpenStorageService;namespace Oss{ /// <summary> /// UploadHandler 的摘要说明 /// </summary> public class UploadHandler : IHttpHandler { OssClient ossClient; String bucketName = "hd3p"; public void ProcessRequest(HttpContext context) { context.Response.ContentType = "application/json;charset=UTF-8"; HttpPostedFile imgFile = context.Request.Files["Filedata"]; if (imgFile != null) { string accessid = "xxxxxx"; // AccessID string accesskey = "xxxxxxxx"; // AccessKey ossClient = new Aliyun.OpenServices.OpenStorageService.OssClient(accessid, accesskey); //当然这里可以封装下 ObjectMetadata meta = new ObjectMetadata(); meta.ContentType = "image/jpeg"; string key = "pic/" imgFile.FileName; PutObjectResult result = ossClient.PutObject(bucketName, key, imgFile.InputStream, meta);//上传图片 AccessControlList accs = ossClient.GetBucketAcl(bucketName); string imgurl = string.Empty; if (!accs.Grants.Any())//判断是否有读取权限 { imgurl = ossClient.GeneratePresignedUri(bucketName, key, DateTime.Now.AddMinutes(5)).AbsoluteUri; //生成一个签名的Uri 有效期5分钟 } else { imgurl = string.Format("http://{0}.oss.aliyuncs.com/{1}", bucketName, key); } context.Response.Write(String.Format("{0}|{1}|{2}|{3}", imgurl, imgurl, imgurl, key));// } } public bool IsReusable { get { return false; } } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复